﻿using DBUtility;
using IYAKASAPortal.Services;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UtilityHelper;


namespace IYAKASAPortal.Statistics
{
    public partial class TJ_JCXM : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        private string m_checkType;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request["CheckType"] != null && !string.IsNullOrEmpty(Request["CheckType"]))
            {
                m_checkType = Request["CheckType"].ToString().Substring(0, 1);
            }
            string userName = Session["UserName"].ToString();
            userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            if (!IsPostBack)
            {
                string d1 = string.Format("{0}", DateTime.Now.ToString("yyyy-MM-dd"));   //默认查询开始时间
                string d2 = string.Format("{0}", DateTime.Now.ToString("yyyy-MM-dd"));   //默认查询截止时间

                txtStart.Text = d1;
                txtEnd.Text = d2;
                btnSearch_Click(null, null);
            }

            if (m_checkType == "Z")
            {
                TableTitle.InnerText = "成都弥荣科技发展有限公司--检验项目分类合格率汇总表(综检)";
                lblTistle.Text = "检验项目分类合格率汇总表(综检)";
            }
            else if (m_checkType == "P")
            {
                TableTitle.InnerText = "成都弥荣科技发展有限公司--检验项目分类合格率汇总表(排放)";
                lblTistle.Text = "检验项目分类合格率汇总表(排放)";
            }
            else
            {
                TableTitle.InnerText = "成都弥荣科技发展有限公司--检验项目分类合格率汇总表(安检)";
                lblTistle.Text = "检验项目分类合格率汇总表(安检)";
            }
            lblJGMC.Text = string.Format("机动车安全技术检验机构名称:{0}", StaInfoTools.GetStationName());

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string startDate = txtStart.Text;
                string endDate = txtEnd.Text;

                //当条件为空的查询范围 加减100年
                DateTime dFuture = DateTime.Now.AddYears(100);
                DateTime dPast = DateTime.Now.AddYears(-100);

                //获取表中的最大最小日期
                string maxDateSql = "SELECT TOP 1 JCRQ FROM RESULT_VEHICLE_INFO WHERE JCRQ IS NOT NULL  ORDER BY JCRQ DESC";
                string minDateSql = "SELECT TOP 1 JCRQ FROM RESULT_VEHICLE_INFO WHERE JCRQ IS NOT NULL  ORDER BY JCRQ ASC";
                string maxDate = DbHelper.GetValue(maxDateSql);
                string minDate = DbHelper.GetValue(minDateSql);

                if (txtStart.Text == string.Empty && txtEnd.Text == string.Empty)
                {
                    endDate = dFuture.ToString("yyyy-MM-dd");
                    startDate = dPast.ToString("yyyy-MM-dd");

                    lblDate.Text = string.Format("{0} - {1}", minDate, maxDate);
                }
                else if (txtStart.Text == string.Empty)
                {
                    startDate = dPast.ToString("yyyy-MM-dd");
                    lblDate.Text = string.Format("{0} - {1}", minDate, endDate);
                }
                else if (txtEnd.Text == string.Empty)
                {
                    endDate = dFuture.ToString("yyyy-MM-dd");
                    lblDate.Text = string.Format("{0} - {1}", minDate, maxDate);
                }
                else
                {
                    lblDate.Text = string.Format("{0} - {1}", startDate, endDate);
                }


                BindDataToList(startDate, endDate);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            try
            {
                txtStart.Text = string.Empty;
                txtEnd.Text = string.Empty;
                btnSearch_Click(null, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);                
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            
        }


        private void BindDataToList(string startDate, string endDate)
        {
            try
            {
              DataTable dt =  GetStatisticsData(startDate,endDate);
              rptList.DataSource = dt;
              rptList.DataBind();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 获取返回的数据，并进行运算
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">截至日期</param>
        /// <returns>返回一个DataTable</returns>
        public DataTable GetStatisticsData(string startDate,string endDate)
        {

            if (Request["CheckType"] != null && !string.IsNullOrEmpty(Request["CheckType"]))
            {
                m_checkType = Request["CheckType"].ToString().Substring(0, 1);
            }

            DataTable dt = new DataTable();
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@startDate",startDate),
                new SqlParameter("@endDate",endDate),
                new SqlParameter("@checkType",m_checkType)
              };
            DbStoredProcedure dbHelper = new DbStoredProcedure();
            dbHelper.GetProcDataTable(ref dt, "proc_RESULT_VEHICLE_INFO_GetJYXMData", param);


            //获取当前方法名称，写入日志
            string funName = string.Format("{0}.aspx.cs -> {1}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, (new StackTrace()).GetFrame(0).GetMethod().Name);
            //WriteLog(string.Empty,"查询安检检测项目数据",userInfo.UserName,LogHelper.LogType.Normal,funName);

            dt.Columns.Add("No",typeof(string));
            dt.Columns.Add("ZJHGL",typeof(string));
            int  countCLS =0 ,countCJHG = 0, countCJBHG = 0, countYCFJHG = 0, countYCFJBHG = 0, countZJHG = 0, countZJBHG = 0; 

            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {
                dr["No"] = i.ToString();
                if (Convert.ToDouble(dr["CLS"]) > 0)
                {
                    dr["ZJHGL"] = Math.Round(Convert.ToDouble(dr["ZJHG"]) * 100 / Convert.ToDouble(dr["CLS"]), 2).ToString() + "%";
                }
                else
                {
                    dr["ZJHGL"] = "0%";
                }

                countCJHG += (int)dr["CJHG"];
                countCJBHG += (int)dr["CJBHG"];
                countYCFJHG += (int)dr["YCFJHG"];
                countYCFJBHG += (int)dr["YCFJBHG"];
                countZJHG += (int)dr["ZJHG"];
                countZJBHG += (int)dr["ZJBHG"];
                countCLS += (int)dr["CLS"];

                i++;
            }

            //合计行
            DataRow tDr = dt.NewRow();
            tDr["No"] = string.Empty;
            tDr["TypeName"] = "合计";
            tDr["CJHG"] = countCJHG;
            tDr["CJBHG"] = countCJBHG;
            tDr["YCFJHG"] = countYCFJBHG;
            tDr["YCFJBHG"] = countYCFJBHG;
            tDr["ZJHG"] = countZJHG;
            tDr["ZJBHG"] = countZJBHG;
            tDr["CLS"] = countCLS;
            if (countCLS > 0)
            {
                tDr["ZJHGL"] = Math.Round((double)countZJHG * 100 / countCLS, 2) + "%";
            }
            else
            {
                tDr["ZJHGL"] = "0%";
            }

            dt.Rows.Add(tDr);
            return dt;
        }

    }
}